package com.itheima_03;

public class BinarySearchDemo {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9,10};
        int num = 3;
        int index = binarySearchMoth(arr,num);
        System.out.println(index);
    }

    public static int binarySearchMoth(int[] arr, int num) {
        int max = arr.length - 1;
        int min = 0;
        while (max>=min){
            int mid = (max+min)>>1;
            if(arr[mid]>num){
                //num在左边
                max = mid -1;
            }else if (arr[mid]<num){
                //num在右边
            min= mid +1;
            }else {
                return mid;
            }
        }
        return -1;
    }
}
